
********************************************************************************
********************************************************************************
********************* THIS IS DO FILE 3 (MCO/FFS) ******************************
******* THIS FILE GENERATES THE CONDITIONAL DENIAL PROBABILITIES ***************
********************************************************************************
********************************************************************************

*Version: 2023 March 29

cap mkdir `"$output/ffs_mco_separation/log/3_denial_probabilities"'
cap mkdir `"$output/ffs_mco_separation/temp"'

cap log close 
log using `"$output/ffs_mco_separation/log/3_denial_probabilities/3_denial_probabilities.smcl"', replace 

di "This run uses code version from 2023/03/29"

use `"$output/data/$line_file"', clear

******************************************************************
**************************** Make Pay Type Var *******************
******************************************************************

gen pay_type_ffs_mco = pay_type
replace pay_type_ffs_mco = 1 if pay_type_ffs_mco == 4 // Medicare is one payer (MA and FFS)
replace pay_type_ffs_mco = 4 if pay_type_ffs_mco == 5 // Commercial is pay_type_ffs_mco == 4

gen pay_type_ffs_mco_st = "Medicare"
replace pay_type_ffs_mco_st = "Medicaid FFS" if pay_type_ffs_mco == 2
replace pay_type_ffs_mco_st = "Medicaid MCO" if pay_type_ffs_mco == 3
replace pay_type_ffs_mco_st = "Commercial" if pay_type_ffs_mco == 4

*select sample first instance 
keep if R_1 == 1 & Y_0 == 0 			 // Keep if at least one resubmission 
assert Y_0 == 0 

gen d_1 = 1-Y_1
gen d_2 = 1-Y_2
gen d_3 = 1-Y_3
gen d_4 = 1-Y_4
gen d_5 = 1-Y_5

// generate bins, bin claim amounts - width of bin = USD 25
gegen bin_C = cut(line_item_value) , at(0(25)250)	
replace bin_C = 250 if bin_C == . 	

********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************
// Conditional probabilty that the first resubmission is paid
preserve 

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_1)  	
	keep state pay_type_ffs_mco visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "1"
		
	save `"$output/ffs_mco_separation/temp/denial_probabilities_bin_second_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_2 == 1 & Y_1 == 0 			 // Keep if at least one resubmission 
assert Y_1 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************
// Conditional probabilty that the second resubmission is paid
preserve 

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_2)
	
	keep state pay_type_ffs_mco visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "2"

	save `"$output/ffs_mco_separation/temp/denial_probabilities_bin_third_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_3 == 1 & Y_2 == 0 			 // Keep if at least one resubmission 
assert Y_2 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************
// Conditional probabilty that the third resubmission is paid

preserve 

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_3) 
	
	keep state pay_type_ffs_mco visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "3"
	
	save `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fourth_submission.dta"', replace 
	
restore 



*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************
// Conditional probabilty that the fourth resubmission is paid
preserve 

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_4)
	
	keep state pay_type_ffs_mco visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "4"
	
	save `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fifth_submission.dta"', replace 
	
restore 


*select sample second instance 
keep if R_4 == 1 & Y_3 == 0 			 // Keep if at least one resubmission 
assert Y_3 == 0 


********************************************************************************************
*** 1. Calculate denial probabilities for every procedure and save data to re-merge later***
********************************************************************************************
// Conditional probabilty that the fifth resubmission is paid
preserve 

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gegen p_denial = mean(d_5) 
	
	keep state pay_type_ffs_mco visit_code_cat p_denial bin_C size_indicator
	
	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: gen observations = _N

	bys state pay_type_ffs_mco visit_code_cat bin_C size_indicator: keep if _n == 1 
	
	gen resubmission = "5"
	
	save `"$output/ffs_mco_separation/temp/denial_probabilities_bin_sixth_submission.dta"', replace 
	
restore 



********Generate means

** Bin 
use `"$output/ffs_mco_separation/temp/denial_probabilities_bin_second_submission.dta"', clear
append using `"$output/ffs_mco_separation/temp/denial_probabilities_bin_third_submission.dta"'
append using `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fourth_submission.dta"'
append using `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fifth_submission.dta"'
append using `"$output/ffs_mco_separation/temp/denial_probabilities_bin_sixth_submission.dta"'

bys state pay_type_ffs_mco visit_code_cat bin size_indicator: gegen mean_p_denial = mean(p_denial) [aweight=observations]

bys state pay_type_ffs_mco visit_code_cat bin size_indicator: gegen total_observations = sum(observations)

sort state pay_type_ffs_mco visit_code_cat bin size_indicator resubmission
by state pay_type_ffs_mco visit_code_cat bin size_indicator: ///
	gen resubmissions_used_str = resubmission[1] + resubmission[2] + resubmission[3] + resubmission[4] + resubmission[5]
bys state pay_type_ffs_mco visit_code_cat bin size_indicator: keep if _n == 1 

gen resubmissions_used = strlen(resubmissions_used_str) 
keep state pay_type_ffs_mco visit_code_cat mean_p_denial total_observations resubmissions_used bin size_indicator
rename mean_p_denial p_denial
save `"$output/ffs_mco_separation/temp/mean_denial_probabilities_bin.dta"', replace 

erase `"$output/ffs_mco_separation/temp/denial_probabilities_bin_second_submission.dta"'
erase `"$output/ffs_mco_separation/temp/denial_probabilities_bin_third_submission.dta"'
erase `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fourth_submission.dta"'
erase `"$output/ffs_mco_separation/temp/denial_probabilities_bin_fifth_submission.dta"'
erase `"$output/ffs_mco_separation/temp/denial_probabilities_bin_sixth_submission.dta"'

log close 


